# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')

class JT_1205:
    result = []
    analysisDescription=['流水号：','音视频总数：']
    subpackageDescription=['消息包总数：','包序号：']
    analysis1205Length=[2,4]
    #音视频资源列表
    source_lists = []
    listDescription=['逻辑通道号：','开始时间：','结束时间：','报警标志：','音视频资源类型：','码流：','存储器类型：','文件大小：']
    list1205Length=[1,6,6,8,1,1,1,4]
    #报警标志位字符串
    alarm_data_str = ""
    Alarm_marker = ['紧急报警', '超速报警', '疲劳驾驶', '危险预警', 'GNSS模块发生故障', 'GNSS天线未接或被剪断', 'GNSS天线短路',
                    '终端电源欠压', '终端电源掉电', '终端LCD或显示器故障', 'TTS模块故障', '摄像头故障', '道路运输证IC卡模块故障',
                    '超速预警', '疲劳驾驶预警', '保留', '保留', '保留', '当天累计驾驶超时', '超时停车', '进出区域', '进出路线',
                    '路段驾驶时间不足/过长', '路线偏离报警', '车辆VSS故障', '车辆油量异常', '车辆被盗', '车辆非法点火',
                    '车辆非法位移', '碰撞预警', '侧翻预警', '非法开门', '视频信号丢失', '视频信号遮挡', '存储单元故障报警',
                    '其他视频设备故障', '客车超员', '异常驾驶行为', '特殊报警录像达到存储阈值', '预留']
    def LogicAisle(self,protocolStr,header):
        #消息体属性
        header_attribute_id=header[1].split(':')[1]
        s=bin(int(header_attribute_id,16))
        sub_package=int(s[2:])
        sub_package=str("%016d"%sub_package)
        print(sub_package)
        sub_package=sub_package[2]
        if(sub_package=="0"):
            pStr=protocolStr
        else:
            header.append(self.subpackageDescription[0]+ protocolStr[0:4]+'\n')
            header.append(self.subpackageDescription[1]+ protocolStr[4:8]+'\n')
            pStr=protocolStr[8:]
        #流水号
        header.append(self.analysisDescription[0] + pStr[0:self.analysis1205Length[0]*2]+ '\n')
        #音视频资源总数
        list_str=pStr[(self.analysis1205Length[0]+self.analysis1205Length[1])*2:]
        list_count=0
        s = pStr[4:12]
        s=str(int(s,16))
        print(s)
        total_num=len(list_str)/56
        total_num=int("%d"%total_num)
        header.append(self.analysisDescription[1] + s  + '\n')
        print ("音视频资源总数："+str(total_num))
        #截取资源列表字符串
        for a in range(total_num):
            source_list=list_str[list_count:(list_count+28*2)]
            self.source_lists.append(source_list)
            print(source_list)
            list_count += 28*2
        #分条处理资源列表
        num = 1
        for b in range(total_num):
            l=self.source_lists[b]
            print(l)
            msg="-------------------第"+str(num)+"条音视频资源列表------------------"+"\n"
            header.append(msg)
            count  = 0
            for i in range(len(self.listDescription)):
                # 开始时间和结束时间
                if i == 1 or i == 2:
                    c_time = 0
                    times = []
                    s = l[count:count + (self.list1205Length[i] * 2)]
                    for j in range(0, 6):
                        time = s[c_time:c_time + 2]
                        times.append(time)
                        c_time = c_time + 2
                    header.append(
                        self.listDescription[i] + times[0] + "-" + times[1] + "-" + times[2] + "-" + times[
                            3] + "-" + times[4] + "-" + times[5] + "\n")
                    count = count + self.list1205Length[i] * 2
                # 报警标志位
                elif i == 3:
                    # 十六进制字符串
                    s =l[count:count + (self.list1205Length[i] * 2)]
                    print("报警标志位:"+s)
                    # 转换为二进制字符串
                    s = bin(int(s, 16))
                    # 位数不足补零
                    n = int(s[2:])
                    n = "%064d" % n
                    s = str(n)
                    print(s)
                    header.append(self.listDescription[i] + s + "\n")
                    self.setalarmnumber(s, header)
                    count = count + self.list1205Length[i] * 2
                # 音视频资源
                elif i == 4:
                    s =l[count:count + (self.list1205Length[i] * 2)]
                    if s == "00":
                        header.append(self.listDescription[i] + s + " 音视频" + "\n")
                    elif s == "01":
                        header.append(self.listDescription[i] + s + " 音频" + "\n")
                    elif s == "02":
                        header.append(self.listDescription[i] + s + " 视频" + "\n")
                    elif s == "03":
                        header.append(self.listDescription[i] + s + " 视频或音频" + "\n")
                    count = count + self.list1205Length[i] * 2
                # 码流类型
                elif i == 5:
                    s = l[count:count + (self.list1205Length[i] * 2)]
                    if s == "00":
                        header.append(self.listDescription[i] + s + " 所有码流" + "\n")
                    elif s == "01":
                        header.append(self.listDescription[i] + s + " 主码流" + "\n")
                    elif s == "02":
                        header.append(self.listDescription[i] + s + " 子码流" + "\n")
                    count = count + self.list1205Length[i] * 2
                # 存储器类型
                elif i == 6:
                    s = l[count:count + (self.list1205Length[i] * 2)]
                    if s == "00":
                        header.append(self.listDescription[i] + s + " 所有存储器" + "\n")
                    elif s == "01":
                        header.append(self.listDescription[i] + s + " 主存储器" + "\n")
                    elif s == "02":
                        header.append(self.listDescription[i] + s + " 灾备存储器" + "\n")
                    count = count + self.list1205Length[i] * 2
                elif i==7:
                    s=l[count:]
                    print("文件大小（字节）："+s)
                    s=str(int(s,16))
                    header.append(self.listDescription[i] + s +"字节"+ "\n")
                else:
                    header.append(self.listDescription[i] + l[count:count + (self.list1205Length[i] * 2)] + "\n")
                    count = count + self.list1205Length[i] * 2
            header.append(msg)
            num+=1
            header.append('\n')
        self.result = header

    def setalarmnumber(self, s, header):
        count = 64
        alarm_data_list = []
        self.alarm_data_str = s
        for i in range(64):
            alarm_data = self.alarm_data_str[count - 1:count]
            if alarm_data == "1":
                if i > 38 or i == 15 or i == 16 or i == 17:
                    pass
                else:
                    header.append(self.Alarm_marker[i] + '\n')
            alarm_data_list.append(alarm_data)
            count -= 1
